﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebsocktChatRoom
{
    /// <summary>
    /// 已连接的WebSocket管理类
    /// </summary>
    public class WebsocketHelper
    {
        private static List<WebsocketClient> _clients = new List<WebsocketClient>();
        /// <summary>
        ///添加一个WebSocket对象
        /// </summary>
        /// <param name="client"></param>
        public static void Add(WebsocketClient client)
        {
            _clients.Add(client);
        }
        /// <summary>
        /// 移除一个WebSocket对象
        /// </summary>
        /// <param name="client"></param>
        public static void Remove(WebsocketClient client)
        {
            _clients.Remove(client);
        }
        /// <summary>
        /// 通过id获取一个WebSocket对象
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public static WebsocketClient Get(string clientId)
        {
            var client = _clients.FirstOrDefault(c=>c.Id == clientId);

            return client;
        }
        /// <summary>
        /// 获取所有WebSocket
        /// </summary>
        /// <returns></returns>
        public static List<WebsocketClient> GetAll()
        {
            return _clients;
        }
        /// <summary>
        /// 根据房间号获取WebSocket
        /// </summary>
        /// <param name="roomNo"></param>
        /// <returns></returns>
        public static List<WebsocketClient> GetRoomClients(string roomNo)
        {
            var client = _clients.Where(c => c.RoomNo == roomNo);
            return client.ToList();
        }
    }
}
